function createTextEditor(txtName) {
	this.id = txtName; // Name our object
	this.textarea = $(this.id); // Store a reference to textarea
	
	this.textarea.onselect = function () {this.storeCursor(this)};
	this.textarea.onchange = function () {this.storeCursor(this)};
	this.textarea.onkeyup = function () {this.storeCursor(this)};
	this.textarea.onclick = function () {this.storeCursor(this)};
	this.textarea.storeCursor = function () {
		if(typeof(this.createTextRange)!="undefined")
			this.cursorPos = document.selection.createRange().duplicate();
	};
	
	this.left_text		= new createSimpleText('left_text', 'testo a sinistra', 'left', this);
	this.right_text		= new createSimpleText('right_text', 'testo a destra', 'right', this);
	this.center_text	= new createSimpleText('center_text', 'testo centrato', 'center', this);
	this.bold_text		= new createSimpleText('bold_text', 'grassetto', 'b', this);
	this.italic_text	= new createSimpleText('italic_text', 'corsivo', 'em', this);
	this.underline		= new createSimpleText('underline', 'sottolineato', 'u', this);
	this.delete_text	= new createSimpleText('delete_text', 'depennato', 'd', this);
	this.highline		= new createSimpleText('highline', 'testo evidenziato', 'h', this);
	
	this.quote			= new createSelectBoxText('quote', 'l\'autore della citazione', 'quote', this);
	this.youtube		= new createSelectBoxText('youtube', 'la didascalia del video', 'youtube', this);
	this.link_url		= new createSelectBoxText('link_url', 'l\'indirizzo del sito (URL)', 'url', this);
	this.font_color		= new createSelectBoxText('font_color', 'colore del testo in esadecimale (i.e. #ff0000)', 'color', this);
	this.font_size		= new createSelectBoxText('font_size', 'la dimensione del testo in pixel (max. 39)', 'size', this);
	
	this.img_chooser	= new createImageText('img_chooser', 'img', this);
	
	this.ulist			= new createMultipleText('ulist', 'elenco non ordinato', 'ulist', 'elem', this);
	this.olist			= new createMultipleText('olist', 'elenco ordinato', 'olist', 'elem', this);
	
	this.updateText = function(tagOpen, tagClose) {
		if(this.textarea.value=="Messaggio") this.textarea.value = "";
		if(typeof(this.textarea.cursorPos)!="undefined" && this.textarea.createTextRange) {
			var cursorPos = this.textarea.cursorPos, stored_length = cursorPos.text.length;
	
			cursorPos.text = (cursorPos.text.charAt(cursorPos.text.length - 1)==' ')? 
				tagOpen+cursorPos.text+tagClose+' ' : tagOpen+cursorPos.text+tagClose;
			if(stored_length == 0)	{
				cursorPos.moveStart("character", -tagClose.length);
				cursorPos.moveEnd("character", -tagClose.length);
				cursorPos.select();
			} else
				this.textarea.focus(cursorPos);
		} else if(typeof(this.textarea.selectionStart)!="undefined") {
			var strt = this.textarea.value.substr(0, this.textarea.selectionStart);
			var selection = this.textarea.value.substr(this.textarea.selectionStart, 
				this.textarea.selectionEnd - this.textarea.selectionStart);
			var fin = this.textarea.value.substr(this.textarea.selectionEnd);
			var cursorPos = this.textarea.selectionStart;
			var scrollPos = this.textarea.scrollTop;
	
			this.textarea.value = strt+tagOpen+selection+tagClose+fin;
	
			if(this.textarea.setSelectionRange) {
				if(selection.length==0)
					this.textarea.setSelectionRange(cursorPos+tagOpen.length, cursorPos+tagOpen.length);
				else
					this.textarea.setSelectionRange(cursorPos, cursorPos+tagOpen.length+selection.length+tagClose.length);
				this.textarea.focus();
			}
			
			this.textarea.scrollTop = scrollPos;
		} else {
			this.textarea.value += tagOpen + tagClose;
			this.textarea.focus(this.textarea.value.length-1);
		}
	}
	
	this.insertSingleTag = function(tag) {
		if(typeof(this.textarea.cursorPos)!="undefined" && this.textarea.createTextRange) {
			var cursorPos = this.textarea.cursorPos;
			cursorPos.text = (cursorPos.text.charAt(cursorPos.text.length - 1)==' ')? tag+' ' : tag;
			cursorPos.select();
		} else if(typeof(this.textarea.selectionStart)!="undefined") {
			var strt = this.textarea.value.substr(0, this.textarea.selectionStart);
			var fin = this.textarea.value.substr(this.textarea.selectionEnd);
			var scrollPos = this.textarea.scrollTop;
	
			this.textarea.value = strt + tag + fin
	
			if(this.textarea.setSelectionRange) {
				this.textarea.focus();
				this.textarea.setSelectionRange(strt.length + tag.length, strt.length + tag.length);
			}
			this.textarea.scrollTop = scrollPos;
		} else {
			this.textarea.value += tag;
			this.textarea.focus(this.textarea.value.length - 1);
		}
	}
}

function createSimpleText(trigger, desc, tag, obj) {
	this.trigger = $(trigger);
	this.trigger._parent = obj;

	this.trigger.tagOpen = '['+tag+']';
	this.trigger.tagClose = '[/'+tag+']';

	this.trigger.onclick = function () {
		this._parent.updateText(this.tagOpen, this.tagClose);
	};
}

function createMultipleText(trigger, desc, tag_ext, tag_int, obj) {
	this.trigger = $(trigger);
	this.trigger._parent = obj;

	this.trigger.tagExtOpen = '['+tag_ext+']';
	this.trigger.tagExtClose = '[/'+tag_ext+']';
	this.trigger.tagIntOpen = '['+tag_int+']';
	this.trigger.tagIntClose = '[/'+tag_int+']';

	this.trigger.onclick = function () {
		this._parent.updateText(this.tagExtOpen, this.tagExtClose);
		var i = 1;
		while((par = prompt("Inserire il "+i+"° elemento dell'"+desc+":"))) {
			this._parent.updateText(this.tagIntOpen+par, this.tagIntClose);
			if(typeof(this._parent.textarea.cursorPos)!="undefined" && this._parent.textarea.createTextRange) {
				var cursorPos = this._parent.textarea.cursorPos, stored_length = cursorPos.text.length;
				cursorPos.moveStart("character", +tagIntClose.length);
				cursorPos.moveEnd("character", +tagIntClose.length);
			} else if(typeof(this._parent.textarea.selectionStart)!="undefined") {
				var cursorPos = this._parent.textarea.selectionStart;
				this._parent.textarea.setSelectionRange(cursorPos+this.tagIntClose.length, cursorPos+this.tagIntClose.length);
			}
			i++;
		}
	};
}

function createSelectBoxText(trigger, desc, tag, obj) {
	this.trigger = $(trigger);
	this.trigger._parent = obj;

	this.trigger.tagOpen = '[' + tag + '=';
	this.trigger.tagClose = '[/' + tag + ']';

	this.trigger.onclick = function () { 
		var par = prompt("Inserire "+desc);
		if(par!=null) 
			this._parent.updateText(this.tagOpen+par+']', this.tagClose);
	};
}

function createImageText(trigger, tag, obj) {
	this.trigger = $(trigger);
	this.trigger._parent = obj;

	this.trigger.tagOpen = '[' + tag + '=';
	this.trigger.tagClose = '[/' + tag + ']';

	this.trigger.onclick = function () {
		var source = "";
		if($('selected_image').value!="false") 
			source = $('selected_image').value;
		else if($('img_url_input').value!="") 
			source = $('img_url_input').value;
		
		this._parent.updateText(this.tagOpen + $('img_caption_input').value +']' + source, this.tagClose);
		$('image_chooser').hide();
	};
}